package cn.edu.scau.cmi.luotiansheng.hibernate.service.Impl;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.luotiansheng.hibernate.domain.Student;
import cn.edu.scau.cmi.luotiansheng.hibernate.domain.Teacher;
import cn.edu.scau.cmi.luotiansheng.hibernate.service.TeacherService;
import cn.edu.scau.cmi.luotiansheng.hibernate.util.HibernateUtil;

public class TeacherServiceImpl implements TeacherService{

	private Session session=HibernateUtil.getSession();
	
	@Override
	public Teacher getTea(Long id) {
		// TODO Auto-generated method stub

		session=HibernateUtil.getSession();

		Transaction tx=session.getTransaction();
		tx.begin();
		Teacher t=session.get(Teacher.class, id);
		
		tx.commit();
		
		return t;
	}

	@Override
	public void delTea(Teacher teacher) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		session.delete(teacher);
		tx.commit();
	}

	@Override
	public void saveTea(Teacher teacher) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		
		
		session.save(teacher);
		tx.commit();
	}
	
	@Override
	public void updateTea(Teacher tea) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		Teacher teacher2=session.get(Teacher.class, tea.getId());
		teacher2.setName(tea.getName());
			
		session.update(teacher2);
		tx.commit();
		
	}
	
	
	@Override
	public void updateStuinfo(Student student) {
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		session.saveOrUpdate(student);
		tx.commit();
		
	}
	

	@Override
	public List<Teacher> getAllTeacher() {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		
		List<Teacher> list=session.createSQLQuery("select * from teacher").addEntity(Teacher.class).list();
		
		tx.commit();
		
		
		return list;
	}

}
